# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : ext.py
@Time    : 2020/2/5 0005 17:05
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
import logging
import time
from logging.handlers import TimedRotatingFileHandler

from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_caching import Cache

from App.setting import BASE_LOG

db = SQLAlchemy()
migrate = Migrate()

cache = Cache(
    config={
        "CACHE_TYPE": "redis",
    }
)


def ext_init(app):
    db.init_app(app)
    migrate.init_app(app, db=db)
    cache.init_app(app)
    formatter = logging.Formatter(
        "[%(asctime)s][%(filename)s:%(lineno)d][%(levelname)s][%(thread)d] - %(message)s")
    handler = TimedRotatingFileHandler(
        "{}/{}-flask.log".format(BASE_LOG, time.strftime("%Y-%m-%d")), when="D", interval=1, backupCount=15,
        encoding="UTF-8", delay=False, utc=True)
    app.logger.addHandler(handler)
    handler.setFormatter(formatter)


if __name__ == "__main__":
    pass
